package com.apes.framework.plugin.form.parser.matcher;

import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.log.timer.Timer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能：属性数组表达式解析器
 *
 * @author xul
 * @create 2018-04-19 19:35
 */
public class AttrsMatcher implements Matcher<String, Map<String, String>> {
    protected final Log logger = LogFactory.getLog(this.getClass());

    private Matcher parent;

    /**
     * 功能：获取识别模式
     */
    @Override
    public String getRegex() {
        return ":";
    }

    @Override
    public Matcher getParent() {
        return parent;
    }

    @Override
    public void setParent(Matcher parent) {
        this.parent = parent;
    }

    /**
     * 功能：模式配置
     *
     * @param content
     */
//    @Timer("解析属性数组表达式")
    @Override
    public Map<String, String> parse(String content) {
        ConditionalMatcher conditionalMatcher = new ConditionalMatcher();
        conditionalMatcher.setParent(this);
        List<String> expressions = conditionalMatcher.parse(content.trim());

        Map<String, String> result = new HashMap<>();
        PolishMatcher polishMatcher = new PolishMatcher();
        polishMatcher.setParent(this);
        expressions.stream().forEach((expression) -> {
            String[] values = StringUtils.split(expression, getRegex());
            String k = values[0].trim().replace("'", "");
            String v = values[1].trim();
            result.put(k, (String) polishMatcher.parse(v));
        });
        return result;
    }

}
